#include <string>
#include <unordered_map>

using std::string;
using std::unordered_map;

class Solution {
public:
    int romanToInt(string s) {
        int ret = 0;
        int i = 0;
        while (i < s.size() - 1) {
            char a = s[i];
            char b = s[i + 1];
            i++;
            switch (a) {
            case 'I':
                if (b == 'V') {
                    ret += 4;
                    i++;
                    continue;
                }
                else if (b == 'X') {
                    ret += 9;
                    i++;
                    continue;
                }
                break;
            case 'X':
                if (b == 'L') {
                    ret += 40;
                    i++;
                    continue;
                }
                else if (b == 'C') {
                    ret += 90;
                    i++;
                    continue;
                }
                break;
            case 'C':
                if (b == 'D') {
                    ret += 400;
                    i++;
                    continue;
                }
                else if (b == 'M') {
                    ret += 900;
                    i++;
                    continue;
                }
                break;
            }
            ret += data[a];
        }
        if (i < s.size()) {
            ret += data[s[i]];
        }
        return ret;
    }
private:
    unordered_map<char, int> data{ {'I',1},{'V',5},{'X',10},{'L',50},{'C',100},{'D',500},{'M',1000} };
};